package com.example.tagsdemo;

import com.example.tagsdemo.dto.GeneratorDTO;
import com.example.tagsdemo.dto.TableDTO;
import com.example.tagsdemo.entity.DBTable;
import com.example.tagsdemo.service.DBService;
import com.example.tagsdemo.utils.ResultVOUtil;
import com.example.tagsdemo.vo.LayuiDataVO;
import com.example.tagsdemo.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping
public class IndexController {

    @Autowired
    private DBService dbService;

    /**
     * 首页
     * @return
     */
    @GetMapping
    public String index(){
        return "createtable";
    }

    /**
     * 创建数据库表
     * @param tableDTO
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    public ResultVO create(TableDTO tableDTO){
        return dbService.createTable(tableDTO);
    }

    /**
     *  跳转数据库表列表
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String code(Model model){
        List<DBTable> list  = dbService.getTables();
        model.addAttribute("list",list);
        return "tablelist";
    }

    /**
     * 表列表数据
     * @return
     */
    @GetMapping("/tableList")
    @ResponseBody
    public LayuiDataVO tableList(){
        List<DBTable> list  = dbService.getTables();
        return ResultVOUtil.layuiData(list,list.size());
    }

    @PostMapping("/conf")
    public String conf(Model model,String[] tableArr){
        String tables = StringUtils.join(tableArr,",");
        model.addAttribute("tables",tables);
        return "generatorcode";
    }


    /**
     * 生成实体类
     * @param dto
     * @return
     */
    @PostMapping("/entity/create")
    @ResponseBody
    public ResultVO entityCreate(@RequestBody GeneratorDTO dto){
        return dbService.create(dto);
//        return ResultVOUtil.success();
    }

}
